package com.sky.web.app;

import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.entity.Orders;
import com.sky.result.PageBean;
import com.sky.result.Result;
import com.sky.service.OrderService;
import com.sky.vo.OrderPaymentVO;
import com.sky.vo.OrderSubmitVO;
import com.sky.vo.OrderVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/order")
@Slf4j
public class AppOrderController {

    @Autowired
    private OrderService orderService;

    //用户下单
    @PostMapping("/submit")
    public Result save(@RequestBody Orders orders) {
        OrderSubmitVO vo = orderService.save(orders);
        return Result.success(vo);
    }

    // 订单支付
    @PutMapping("/payment")
    public Result payment(@RequestBody OrdersPaymentDTO ordersPaymentDTO) throws Exception {
        log.info("订单支付：{}", ordersPaymentDTO);
        OrderPaymentVO orderPaymentVO = orderService.payment(ordersPaymentDTO);
        log.info("生成预支付交易单：{}", orderPaymentVO);
        return Result.success(orderPaymentVO);
    }

    //历史订单查询
    @GetMapping("/historyOrders")
    public Result historyOrders(OrdersPageQueryDTO ordersOrderDTO) {
        log.info("历史订单查询");
        PageBean pageBean = orderService.findByPage(ordersOrderDTO);
        return Result.success(pageBean);
    }

    //订单详情
    @GetMapping("/orderDetail/{id}")
    public Result detail(@PathVariable("id") Long orderId) {
        log.info("订单详情查询");
        OrderVO orderVO=orderService.findById(orderId);
        return Result.success(orderVO);
    }

    //取消订单
    @PutMapping("/cancel/{id}")
    public Result cancel(@PathVariable("id") Long orderId) {
        log.info("取消订单");
        orderService.cancelById(orderId);
        return Result.success();
    }

    //再来一单
    @PostMapping("/repetition/{id}")
    public Result repetition(@PathVariable("id") Long orderId) {
        log.info("再来一单");
        orderService.repetitionById(orderId);
        return Result.success();
    }

    //催单
    @GetMapping("/reminder/{id}")
    public Result reminder(@PathVariable("id") Long orderId) {
        log.info("催单");
        orderService.reminder(orderId);
        return Result.success();
    }
}
